'''
Created on Sep 7, 2012

@author: DuongThanh
'''
import csv 
from numpy.ma.core import floor
from numpy.lib.scimath import log10
class OutputResult(object):
    '''
    This class we used to output the result 
    '''
    def __init__(self):
        pass
    
    def ConvertFromNumberToLabels(self,inputFile):
        file = open(inputFile)
        spamreader = csv.reader(file,delimiter = ",")
        result = []
        for line in spamreader:
            leng = len(line)
            pred = line[leng-1]
            i = int(pred)
            value= ""
            if (i==1): value = "population" 
            if (i==2): value = "intervention"
            if (i==3): value = "background"
            if (i==4): value = "outcome"
            if (i==5): value = "study design"
            if (i==6): value = "other"
            result.append(value)
        file.close()
        return result
    
    def f(self,number, sigfig):
        return ("%.15f" % (round(number, int(-1 * floor(log10(number)) + (sigfig - 1))))).rstrip("0").rstrip(".")
    
    def outputBaseOnSentenceLevelLabels(self,outputFileName,sequenceLabel):
        originalTestFile  =  "../../data/test.csv"
        
        file3 = open(outputFileName,'w')
        file4 = open(originalTestFile)
        count = 0
        for line in file4:
            line=line.strip()
            count +=1
            if (count ==1 ): # header line 
                file3.write(line+", prediction"+"\n")
            if (count>1):
                index = (count - 2) / 6;
                label = sequenceLabel[index].strip()
                # find the label of file
                test_label = line.split(',')[0].strip()
                if (test_label == label):
                    file3.write(line+","+"Yes"+"\n")
                else:
                    file3.write(line+","+"No"+"\n")
        file4.close()
        file3.close()        